import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/components/exam_card_dot/error_dot.dart';
import 'package:kxy_flutter_app/components/exam_card_dot/right_dot.dart';
import 'package:kxy_flutter_app/components/exam_card_dot/un_answer_dot.dart';
import 'package:kxy_flutter_app/model/exam_question.dart';

class QuestionResultCardWidget extends StatelessWidget {
  final Function(int) onSelect;
  final List<Question> quesList;

  const QuestionResultCardWidget({
    Key key,
    @required this.quesList,
    @required this.onSelect,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text('答题卡'),
        leading: IconButton(
          icon: Icon(Icons.close),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      body: ListView(
        padding: EdgeInsets.all(20),
        children: [
          counter(),
          header(),
          quesListWidgt(context),
        ],
      ),
    );
  }

  Widget counter() {
    int rightCount = 0;
    int errorCount = 0;
    int nullCount = 0;

    for (var item in quesList) {
      if (item.userAnswer.isEmpty) {
        nullCount++;
        continue;
      }

      if (isRight(item)) {
        rightCount++;
      } else {
        errorCount++;
      }
    }

    return Container(
      alignment: Alignment.centerLeft,
      child: Text('本次作答共计${quesList.length}题，答对$rightCount题，答错$errorCount题，未答$nullCount题。'),
    );
  }

  Widget header() {
    return Container(
      margin: EdgeInsets.only(top: 20),
      child: Row(
        children: [
          Row(
            children: [
              UnAnswerDot(),
              Padding(
                padding: EdgeInsets.only(left: 10),
                child: RightDot(),
              ),
              Padding(
                padding: EdgeInsets.only(left: 10),
                child: ErrorDot(),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget quesListWidgt(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 20),
      child: Wrap(
        spacing: 20,
        runSpacing: 15,
        children: quesList.map((item) {
          var index = quesList.indexOf(item);
          return quesCardItem(index, item, context);
        }).toList(),
      ),
    );
  }

  Widget quesCardItem(int index, Question ques, BuildContext context) {
    Color labelColor;
    Decoration decoration;
    if (ques.answered) {
      labelColor = Colors.white;
      if (isRight(ques)) {
        decoration = rightDecoration();
      } else {
        decoration = errorDecoration();
      }
    } else {
      labelColor = Color.fromRGBO(136, 136, 136, 1);
      decoration = unAnsweredDecoration();
    }

    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop();
        onSelect(index);
      },
      child: Container(
        height: 36,
        width: 36,
        alignment: Alignment.center,
        decoration: decoration,
        child: Text(
          (index + 1).toString(),
          style: TextStyle(color: labelColor),
        ),
      ),
    );
  }

  Decoration rightDecoration() {
    return BoxDecoration(
      shape: BoxShape.circle,
      color: Color.fromRGBO(102, 211, 159, 1),
      border: Border.all(
        color: Color.fromRGBO(93, 186, 138, 1),
      ),
    );
  }

  Decoration errorDecoration() {
    return BoxDecoration(
      shape: BoxShape.circle,
      color: Color.fromRGBO(255, 142, 128, 1),
      border: Border.all(
        color: Color.fromRGBO(252, 119, 101, 1),
      ),
    );
  }

  Decoration unAnsweredDecoration() {
    return BoxDecoration(
      shape: BoxShape.circle,
      color: Colors.white,
      border: Border.all(
        color: Color.fromRGBO(204, 204, 204, 1),
      ),
    );
  }

  bool isRight(Question ques) {
    if (ques.type == 4 /* 填空题 */) {
      return isRightForFillBlankQues(ques);
    }
    return ques.userAnswer == ques.rightAnswer;
  }

  bool isRightForFillBlankQues(Question ques) {
    List<List<String>> rightAnswerList = new List<List<String>>();
    List<String> userAnswerList = new List<String>();
    if (ques.rightAnswer.isNotEmpty) {
      var json = jsonDecode(ques.rightAnswer);
      (json as List).forEach((items) {
        rightAnswerList.add((items as List).map((e) => e.toString()).toList());
      });
    }

    if (ques.userAnswer.isNotEmpty) {
      var json = jsonDecode(ques.userAnswer);
      (json as List).forEach((item) {
        userAnswerList.add(item);
      });
    }

    int rightCount = 0;
    if (ques.answDisorder) {
      for (var i = 0; i < userAnswerList.length; i++) {
        var userAnswer = userAnswerList[i];

        int rightIndex = -1;
        for (var n = 0; n < rightAnswerList.length; n++) {
          var rightAnswerItems = rightAnswerList[n];
          if (rightAnswerItems.contains(userAnswer)) {
            rightIndex = n;
            rightCount++;
          }
        }

        if (rightIndex > -1) {
          rightAnswerList.removeAt(rightIndex);
        }
      }
    } else {
      for (var i = 0; i < userAnswerList.length; i++) {
        var userAnswer = userAnswerList[i];
        var rightAnswerItems = rightAnswerList[i];
        if (rightAnswerItems.contains(userAnswer)) {
          rightCount++;
        }
      }
    }

    return rightCount == ques.blankCount;
  }
}
